class Dep {
  effects = new Set();
  depend() {
    if (currentEffect) {
      this.effects.add(currentEffect);
    }
  }
  notify() {
    this.effects.forEach(effect => effect());
  }
}

let currentEffect;
// 所有对象的Dep映射表
const depsMap = new WeakMap();

export default function getDep(target, key) {
  let deps = depsMap.get(target);
  if (!deps) {
    deps = new Map();
    depsMap.set(target, deps);
  }

  let dep = deps.get(key);
  if (!dep) {
    dep = new Dep();
    deps.set(key, dep);
  }
  return dep;
}

export function watchEffect(cb) {
  currentEffect = cb;
  cb();
  currentEffect = null;
}
